home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 8_6.lha / 8_6 / in.c < prev    next >
Text File  |  1993-08-08  |  5KB  |  367 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. *ident    "@(#)cfront:lib/stream/in.c    1.8" */
  6. *
  7. C++ stream i/o source
  8.  
  9. in.c
  10. /
  11. include <ctype.h>
  12. include <stream.h>
  13. / #include <common.h>
  14.  
  15. * predefined whitespace */
  16. hitespace WS;
  17.  
  18. / inline
  19. oid eatwhite (istream& is)
  20.  
  21. if (is.tied_to) is.tied_to->flush();
  22. register streambuf *nbp = is.bp;
  23. register c = nbp->sgetc();
  24. while (isspace(c&0377)) c = nbp->snextc();
  25. if (c == EOF) is.state |= _eof;
  26.  
  27.  
  28. stream& istream::operator>>(whitespace&)
  29.  
  30. register streambuf *nbp = bp;
  31.  
  32. if (state) return *this;
  33. if (tied_to) tied_to->flush();
  34. register c = nbp->sgetc();
  35. while (isspace(c)) c = nbp->snextc();
  36. if (c == EOF) state |= _eof;
  37. return *this;
  38.  
  39.  
  40. stream& istream::operator>>(register char& s)
  41. *
  42. reads characters NOT very small integers
  43. */
  44.  
  45. if (skipws)
  46.     eatwhite(*this);
  47. else if (tied_to)
  48.     tied_to->flush();
  49.  
  50. if (state) {
  51.     state |= _fail;
  52.     return *this;
  53. }
  54.  
  55. register c = bp->sgetc();
  56. if (c == EOF) {
  57.     state |= _fail|_eof;
  58. } else {
  59.     s = c;
  60.     bp->stossc();
  61. }
  62.  
  63. return *this;
  64.  
  65.  
  66. stream& istream::operator>>(register char* s)
  67.  
  68. register streambuf *nbp = bp;
  69.  
  70.  
  71. if (skipws)
  72.     eatwhite(*this);
  73. else if (tied_to)
  74.     tied_to->flush();
  75.  
  76.  
  77. if (state) {
  78.     state |= _fail;
  79.     return *this;
  80. }
  81.  
  82. /* get string */
  83. register c = nbp->sgetc();
  84. if (c == EOF) state |= _fail;
  85. while (!isspace(c) && c != EOF) {
  86.     *s++ = c;
  87.     c = nbp->snextc();
  88. }
  89. *s = '\0';
  90.  
  91. if (c == EOF) state |= _eof;
  92.  
  93. return *this;
  94.  
  95.  
  96. stream& istream::operator>>(long& i)
  97.  
  98. register c;
  99. register ii = 0;
  100. register streambuf *nbp = bp;
  101. int neg = 0;
  102.  
  103. if (skipws)
  104.     eatwhite(*this);
  105. else if (tied_to)
  106.     tied_to->flush();
  107.  
  108.  
  109. if (state) {
  110.     state |= _fail;
  111.     return *this;
  112. }
  113.  
  114. switch (c = nbp->sgetc()) {
  115. case '-':
  116. case '+':
  117.     neg = c;
  118.     c = nbp->snextc();
  119.     break;
  120. case EOF:
  121.     state |= _fail;
  122. }
  123.  
  124. if (isdigit(c)) {
  125.     do {
  126.         ii = ii*10+c-'0';
  127.     } while (isdigit(c=nbp->snextc()));
  128.     i = (neg=='-') ? -ii : ii;
  129. } else
  130.     state |= _fail;
  131.  
  132. if (c == EOF) state |= _eof;
  133. return *this;
  134.  
  135.  
  136. stream& istream::operator>>(int& i)
  137.  
  138. long l;
  139.  
  140. if (skipws)
  141.     eatwhite(*this);
  142. else if (tied_to)
  143.     tied_to->flush();
  144.  
  145. if (state) {
  146.     state |= _fail;
  147.     return *this;
  148. }
  149.  
  150. if ( *this>>l ) {
  151.     i = (int)l;
  152. }
  153. return *this;
  154.  
  155.  
  156. stream& istream::operator>>(short& i)
  157.  
  158. long l;
  159.  
  160. if (skipws)
  161.     eatwhite(*this);
  162. else if (tied_to)
  163.     tied_to->flush();
  164.  
  165.  
  166. if (state) {
  167.     state |= _fail;
  168.     return *this;
  169. }
  170.  
  171. if ( *this>>l ) {
  172.     i = (short)l;
  173. }
  174. return *this;
  175.  
  176.  
  177. stream& istream::operator>>(double& d)
  178. *
  179. {+|-} d* {.} d* { e|E {+|-} d+ }
  180. except that
  181.     - a dot must be pre- or succeded by at least one digit
  182.     - an exponent must be preseded by at least one digit
  183. /
  184.  
  185. register c = 0;
  186. char buf[256];
  187. register char* p = buf;
  188. register streambuf* nbp = bp;
  189.  
  190. if (skipws)
  191.     eatwhite(*this);
  192. else if (tied_to)
  193.     tied_to->flush();
  194.  
  195. if (state) {
  196.     state |= _fail;
  197.     return *this;
  198. }
  199.  
  200. /* get the sign */
  201. switch (c = nbp->sgetc()) {
  202. case EOF:
  203.     state = _eof|_fail;
  204.     return *this;
  205. case '-':
  206. case '+':
  207.     *p++ = c;
  208.     c = bp->snextc();
  209. }
  210.  
  211. /* get integral part */
  212. while (isdigit(c)) {
  213.     *p++ = c;
  214.     c = bp->snextc();
  215. }
  216.  
  217. /* get fraction */
  218. if (c == '.') {
  219.     do {
  220.         *p++ = c;
  221.         c = bp->snextc();
  222.     } while (isdigit(c));
  223. }
  224.  
  225. /* get exponent */
  226. if (c == 'e' || c == 'E') {
  227.     *p++ = c;
  228.     switch (c = nbp->snextc()) {
  229.     case EOF:
  230.         state = _eof|_fail;
  231.         return *this;
  232.     case '-':
  233.     case '+':
  234.         *p++ = c;
  235.         c = bp->snextc();
  236.     }
  237.     while (isdigit(c)) {
  238.         *p++ = c;
  239.         c = bp->snextc();
  240.     }
  241. }
  242.  
  243. *p = 0;
  244. d = atof(buf);
  245.  
  246. if (c == EOF) state |= _eof;
  247. return *this;
  248.  
  249.  
  250. stream& istream::operator>>(float& f)
  251.  
  252. double d;
  253.  
  254. if (skipws)
  255.     eatwhite(*this);
  256. else if (tied_to)
  257.     tied_to->flush();
  258.  
  259. if (state) {
  260.     state |= _fail;
  261.     return *this;
  262. }
  263.  
  264. if ( *this>>d ) {
  265.     f = d;
  266. }
  267. return *this;
  268.  
  269.  
  270. stream& istream::get(
  271. register char* s,    /* character array to read into */
  272. register int len,    /* size of character array */
  273. register char term    /* character that terminates input */
  274.  {
  275. register c;
  276. register streambuf *nbp = bp;
  277.  
  278. if (tied_to) tied_to->flush();
  279.  
  280. if (state) {
  281.     state |= _fail;
  282.     return *this;
  283. }
  284.  
  285. if ((c = bp->sgetc()) == EOF) {
  286.     state |= _fail | _eof;
  287.     return *this;
  288. }
  289.  
  290. while (c != term && c != EOF && len > 1) {
  291.     *s++ = c;
  292.     c = nbp->snextc();
  293.     len--;
  294. }
  295. *s = '\0';
  296. if (c == EOF) state |= _eof;
  297. return *this;
  298.  
  299.  
  300. stream& istream::putback(register char c)
  301.  
  302. bp->sputbackc(c);
  303. return *this;
  304.  
  305.  
  306. stream& istream::get(
  307. register streambuf &s,    /* streambuf to input to */
  308. register char term    /* termination character */
  309.  {
  310. register c;
  311. register streambuf *nbp = bp;
  312.  
  313. if (tied_to) tied_to->flush();
  314.  
  315. if (state) {
  316.     state |= _fail;
  317.     return *this;
  318. }
  319.  
  320. if ((c = bp->sgetc()) == EOF) {
  321.     state |= _fail | _eof;
  322.     return *this;
  323. }
  324.  
  325. while (c != term && c != EOF) {
  326.     if (s.sputc(c) == EOF) break;
  327.     c = nbp->snextc();
  328. }
  329. if (c == EOF) state |= _eof;
  330. return *this;
  331.  
  332.  
  333. stream& istream::operator>>(register streambuf &s)
  334.  
  335. register c;
  336. register streambuf *nbp = bp;
  337.  
  338. if (tied_to) tied_to->flush();
  339.  
  340. if (state) {
  341.     state |= _fail;
  342.     return *this;
  343. }
  344.  
  345. if ((c = bp->sgetc()) == EOF) {
  346.     state |= _fail | _eof;
  347.     return *this;
  348. }
  349.  
  350. while (c != EOF) {
  351.     if (s.sputc(c) == EOF) break;
  352.     c = nbp->snextc();
  353. }
  354. if (c == EOF) state |= _eof;
  355. return *this;
  356.  
  357.  
  358. /istream& istream::operator>>(common& p)
  359. /{
  360. /    if (skipws)
  361. /        eatwhite(*this);
  362. /    else if (tied_to)
  363. /        tied_to->flush();
  364. /
  365. /    return p.read(*this);
  366. /}
  367.